######################################################################
#CharCreationGUI.py
#
#Purpose:  Graphical inteface for creating a CMRPG character.
#Author:  Cody Jackson
#Date:  8/23/06
#
#Copyright 2006 Cody Jackson
#This program is free software; you can redistribute it and/or modify it 
#under the terms of the GNU General Public License as published by the Free 
#Software Foundation; either version 2 of the License, or (at your option) 
#any later version.
#
#This program is distributed in the hope that it will be useful, but 
#WITHOUT ANY WARRANTY; without even the implied warranty of 
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
#General Public License for more details.
#
#You should have received a copy of the GNU General Public License 
#along with this program; if not, write to the Free Software Foundation, 
#Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#----------------------------------------------------------------------
#Version 1.0
#   Initial build
######################################################################

#!/usr/bin/env python
# -*- coding: ISO-8859-1 -*-
# generated by wxGlade 0.4 on Thu Aug 24 13:02:08 2006

import wx
import os
import cPickle
from dice_roller import multiDie

if os.path.isdir("C:\\Marines"):
        pass
else: os.mkdir("C:\\Marines")

class CharInfo(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: CharInfo.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.panel_1 = wx.Panel(self, -1)
        self.lblName = wx.StaticText(self.panel_1, -1, "Name:")
        self.enterNameBox = wx.TextCtrl(self.panel_1, -1, "")
        self.radioGender = wx.RadioBox(self.panel_1, -1, "Gender:", choices=["Male", "Female"], majorDimension=2, style=wx.RA_SPECIFY_ROWS)
        self.btnNext1 = wx.Button(self.panel_1, -1, "Next Page")

        self.__set_properties()
        self.__do_layout()
        # end wxGlade

#---Bind events
        self.Bind(wx.EVT_TEXT, self.getName, self.enterNameBox)
        self.Bind(wx.EVT_BUTTON, self.pushedNext1, self.btnNext1)
        #self.Bind(wx.EVT_RADIOBOX, self.genderChoice, self.radioGender)

#---Define events
    def getName(self, event):
        """Get character name."""
        
        self.name = event.GetString()
    
    def pushedNext1(self, event):
        """Move to next screen."""
        
        #---Make save file
        try:
            self.savename = self.name + ".cmc"  #.cmc is extension for character files
            self.location = os.path.join("C:\\", "Marines", self.savename)
            self.charSave = open(self.location, "w")
        except IOError, message:
            print >> sys.stderr, "File could not be opened: ", message
            sys.exit(1)
        
        #---Get and convert gender value
        self.gender = self.radioGender.GetSelection()
        if self.gender == 0:
            self.gender = "Male"
        elif self.gender == 1:
            self.gender = "Female"
        else: print "Wrong gender passed!"
        
        #---Save values to file
        cPickle.dump(self.name, self.charSave)
        cPickle.dump(self.gender, self.charSave)
        
        #---Open next window
        page2 = Attributes(None, -1, "")
        page2.Show()
        self.Destroy()  #close this window

    def __set_properties(self):
        # begin wxGlade: CharInfo.__set_properties
        self.SetTitle("Character Creation")
        self.enterNameBox.SetMinSize((200, -1))
        self.radioGender.SetSelection(0)
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: CharInfo.__do_layout
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        sizer_2 = wx.BoxSizer(wx.VERTICAL)
        sizer_2.Add(self.lblName, 0, wx.ADJUST_MINSIZE, 0)
        sizer_2.Add(self.enterNameBox, 0, wx.ADJUST_MINSIZE, 0)
        sizer_2.Add(self.radioGender, 0, wx.ADJUST_MINSIZE, 0)
        sizer_2.Add((20, 20), 0, wx.ADJUST_MINSIZE, 0)
        sizer_2.Add(self.btnNext1, 0, wx.ADJUST_MINSIZE, 0)
        self.panel_1.SetAutoLayout(True)
        self.panel_1.SetSizer(sizer_2)
        sizer_2.Fit(self.panel_1)
        sizer_2.SetSizeHints(self.panel_1)
        sizer_1.Add(self.panel_1, 1, wx.EXPAND, 0)
        self.SetAutoLayout(True)
        self.SetSizer(sizer_1)
        sizer_1.Fit(self)
        sizer_1.SetSizeHints(self)
        self.Layout()
        # end wxGlade

# end of class CharInfo


class Attributes(CharInfo, wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: Attributes.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.panel_2 = wx.Panel(self, -1)
        self.btnAttributes = wx.Button(self.panel_2, -1, "Get Attributes")
        self.lblAge = wx.StaticText(self.panel_2, -1, "Age:")
        self.txtAge = wx.TextCtrl(self.panel_2, -1, "")
        self.lblChar = wx.StaticText(self.panel_2, -1, "Charisma:")
        self.txtChar = wx.TextCtrl(self.panel_2, -1, "")
        self.lblStr = wx.StaticText(self.panel_2, -1, "Strength:")
        self.txtStr = wx.TextCtrl(self.panel_2, -1, "")
        self.lblBuild = wx.StaticText(self.panel_2, -1, "Build:")
        self.txtBuild = wx.TextCtrl(self.panel_2, -1, "")
        self.lblIntel = wx.StaticText(self.panel_2, -1, "Intelligence:")
        self.txtIntel = wx.TextCtrl(self.panel_2, -1, "")
        self.lblThrow = wx.StaticText(self.panel_2, -1, "Throw Range:")
        self.txtThrow = wx.TextCtrl(self.panel_2, -1, "")
        self.lblWill = wx.StaticText(self.panel_2, -1, "Willpower:")
        self.txtWill = wx.TextCtrl(self.panel_2, -1, "")
        self.lblMass = wx.StaticText(self.panel_2, -1, "Mass:")
        self.txtMass = wx.TextCtrl(self.panel_2, -1, "")
        self.lblDex = wx.StaticText(self.panel_2, -1, "Dexterity:")
        self.txtDex = wx.TextCtrl(self.panel_2, -1, "")
        self.lblLoad = wx.StaticText(self.panel_2, -1, "Load:")
        self.txtLoad = wx.TextCtrl(self.panel_2, -1, "")
        self.lblHP = wx.StaticText(self.panel_2, -1, "Hit Points")
        self.lblHead = wx.StaticText(self.panel_2, -1, "Head:")
        self.txtHead = wx.TextCtrl(self.panel_2, -1, "")
        self.lblTorso = wx.StaticText(self.panel_2, -1, "Torso:")
        self.txtTorso = wx.TextCtrl(self.panel_2, -1, "")
        self.lblRArm = wx.StaticText(self.panel_2, -1, "Right Arm:")
        self.txtRArm = wx.TextCtrl(self.panel_2, -1, "")
        self.lblLArm = wx.StaticText(self.panel_2, -1, "Left Arm:")
        self.txtLArm = wx.TextCtrl(self.panel_2, -1, "")
        self.lblRLeg = wx.StaticText(self.panel_2, -1, "Right Leg:")
        self.txtRLeg = wx.TextCtrl(self.panel_2, -1, "")
        self.lblLLeg = wx.StaticText(self.panel_2, -1, "Left Leg:")
        self.txtLLeg = wx.TextCtrl(self.panel_2, -1, "")
        self.btnNext2 = wx.Button(self.panel_2, -1, "Next Page")

        self.__set_properties()
        self.__do_layout()
        # end wxGlade
        
        #---Create storage containers
        self.attribDict = {}
        self.hpDict = {}
        
        #---Bind events
        self.Bind(wx.EVT_BUTTON, self.getAttributes, self.btnAttributes)
        self.Bind(wx.EVT_BUTTON, self.pushedNext2, self.btnNext2)
    
    def getAttributes(self, event):
        """Generate attribute values."""
        
        self.setAttributes()
        self.setHitPoints()
        
        #---Clear text display boxes, if needed
        self.txtAge.SetValue("")
        self.txtBuild.SetValue("")
        self.txtChar.SetValue("")
        self.txtDex.SetValue("")
        self.txtIntel.SetValue("")
        self.txtLoad.SetValue("")
        self.txtMass.SetValue("")
        self.txtStr.SetValue("")
        self.txtThrow.SetValue("")
        self.txtWill.SetValue("")
        self.txtHead.SetValue("")
        self.txtTorso.SetValue("")
        self.txtRArm.SetValue("")
        self.txtLArm.SetValue("")
        self.txtRLeg.SetValue("")
        self.txtLLeg.SetValue("")
        
        #---Convert integer values to strings for display
        self.ageString = str(self.getAge())
        self.buildString = str(self.attribDict['build'])
        self.strengthString = str(self.attribDict['str'])
        self.intelString = str(self.attribDict['intel'])
        self.charismaString = str(self.attribDict['char'])
        self.willString = str(self.attribDict['will'])
        self.dexString = str(self.attribDict['dex'])
        self.throwString = str(self.attribDict['throw'])
        self.massString = str(self.attribDict['mass'])
        self.loadString = str(self.attribDict['load'])
        self.headString = str(self.hpDict['head'])
        self.torsoString = str(self.hpDict['torso'])
        self.rArmString = str(self.hpDict['rarm'])
        self.lArmString = str(self.hpDict['larm'])
        self.rLegString = str(self.hpDict['rleg'])
        self.lLegString = str(self.hpDict['lleg'])
        
        #--Display values
        self.txtAge.SetValue(self.ageString)
        self.txtBuild.SetValue(self.buildString)
        self.txtStr.SetValue(self.strengthString)
        self.txtIntel.SetValue(self.intelString)
        self.txtThrow.SetValue(self.throwString)
        self.txtDex.SetValue(self.dexString)
        self.txtChar.SetValue(self.charismaString)
        self.txtLoad.SetValue(self.loadString)
        self.txtMass.SetValue(self.massString)
        self.txtWill.SetValue(self.willString)
        self.txtHead.SetValue(self.headString)
        self.txtTorso.SetValue(self.torsoString)
        self.txtRArm.SetValue(self.rArmString)
        self.txtLArm.SetValue(self.lArmString)
        self.txtRLeg.SetValue(self.rLegString)
        self.txtLLeg.SetValue(self.lLegString)

    def getAge(self):
        """Calculate the age of character, between 18 and 45 years old."""
        
        self.age = 15 + multiDie(3,2) #18-45 years old
        return self.age
    
    def setAttributes(self):
        """Calculate values for attributes between 2 and 20."""
        
        self.attribDict = {'str':0, 'will':0, 'intel':0, 'char':0, 'build':0, 'dex':0}
        for x in self.attribDict:
            self.attribDict[x] = multiDie(2,2)
        self.attribDict['throw'] = self.attribDict['str'] * 2
        self.attribDict['mass'] = (self.attribDict['build'] * 5) + 15
        self.attribDict['load'] = self.attribDict['str'] + self.attribDict['build']
    
    def setHitPoints(self):
        """Calculate hit points for each body part, based on "build" attribute."""
        
        self.hpDict = {'larm':0, 'rarm':0, 'lleg':0, 'rleg':0}
        for x in self.hpDict:
            self.hpDict[x] = self.attribDict['build'] * 2
        self.hpDict['head'] = self.attribDict['build']
        self.hpDict['torso'] = self.attribDict['build'] * 4
    
    def pushedNext2(self, event):
        """Move to next screen."""
        
        #---Save values to file
        #cPickle.dump(self.age, self.charSave)
        #cPickle.dump(self.attribDict, )
        #cPickle.dump(self.hpDict, )
        
        #---Open next window
        #page3 = Skills(None, -1, "")
        #page3.Show()
        self.Destroy()  #close this window
        
    def __set_properties(self):
        # begin wxGlade: Attributes.__set_properties
        self.SetTitle("Attributes")
        self.txtAge.SetMinSize((30, -1))
        self.txtChar.SetMinSize((30, -1))
        self.txtStr.SetMinSize((30, -1))
        self.txtBuild.SetMinSize((30, -1))
        self.txtIntel.SetMinSize((30, -1))
        self.txtThrow.SetMinSize((30, -1))
        self.txtWill.SetMinSize((30, -1))
        self.txtMass.SetMinSize((30, -1))
        self.txtDex.SetMinSize((30, -1))
        self.txtLoad.SetMinSize((30, -1))
        self.lblHP.SetFont(wx.Font(8, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
        self.txtHead.SetMinSize((30, -1))
        self.txtTorso.SetMinSize((30, -1))
        self.txtRArm.SetMinSize((30, -1))
        self.txtLArm.SetMinSize((30, -1))
        self.txtRLeg.SetMinSize((30, -1))
        self.txtLLeg.SetMinSize((30, -1))
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: Attributes.__do_layout
        sizer_3 = wx.BoxSizer(wx.VERTICAL)
        sizer_6 = wx.BoxSizer(wx.VERTICAL)
        grid_sizer_2 = wx.FlexGridSizer(4, 5, 0, 0)
        grid_sizer_1 = wx.FlexGridSizer(5, 5, 0, 0)
        sizer_6.Add(self.btnAttributes, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ADJUST_MINSIZE, 0)
        sizer_6.Add((5, 5), 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add(self.lblAge, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add(self.txtAge, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add((20, 20), 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add(self.lblChar, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add(self.txtChar, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add(self.lblStr, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add(self.txtStr, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add((20, 20), 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add(self.lblBuild, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add(self.txtBuild, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add(self.lblIntel, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add(self.txtIntel, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add((20, 20), 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add(self.lblThrow, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add(self.txtThrow, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add(self.lblWill, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add(self.txtWill, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add((20, 20), 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add(self.lblMass, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add(self.txtMass, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add(self.lblDex, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add(self.txtDex, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add((20, 20), 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add(self.lblLoad, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add(self.txtLoad, 0, wx.ADJUST_MINSIZE, 0)
        sizer_6.Add(grid_sizer_1, 1, wx.EXPAND, 0)
        sizer_6.Add(self.lblHP, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_2.Add((20, 20), 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_2.Add(self.lblHead, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_2.Add((20, 20), 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_2.Add(self.txtHead, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_2.Add((20, 20), 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_2.Add((20, 20), 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_2.Add(self.lblTorso, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_2.Add((20, 20), 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_2.Add(self.txtTorso, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_2.Add((20, 20), 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_2.Add(self.lblRArm, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_2.Add(self.txtRArm, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_2.Add((20, 20), 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_2.Add(self.lblLArm, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_2.Add(self.txtLArm, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_2.Add(self.lblRLeg, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_2.Add(self.txtRLeg, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_2.Add((20, 20), 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_2.Add(self.lblLLeg, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_2.Add(self.txtLLeg, 0, wx.ADJUST_MINSIZE, 0)
        sizer_6.Add(grid_sizer_2, 1, wx.EXPAND, 0)
        sizer_6.Add(self.btnNext2, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ADJUST_MINSIZE, 0)
        self.panel_2.SetAutoLayout(True)
        self.panel_2.SetSizer(sizer_6)
        sizer_6.Fit(self.panel_2)
        sizer_6.SetSizeHints(self.panel_2)
        sizer_3.Add(self.panel_2, 1, wx.EXPAND, 0)
        self.SetAutoLayout(True)
        self.SetSizer(sizer_3)
        sizer_3.Fit(self)
        sizer_3.SetSizeHints(self)
        self.Layout()
        # end wxGlade

# end of class Attributes

class Character(wx.App):
    def OnInit(self):
        wx.InitAllImageHandlers()
        charInfo = CharInfo(None, -1, "")
        self.SetTopWindow(charInfo)
        charInfo.Show()
        return 1

# end of class Character

if __name__ == "__main__":
    charCreaterGUI = Character(0)
    charCreaterGUI.MainLoop()
